package com.wythe.mall.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wythe.mall.BaseApplication;
import com.wythe.mall.R;
import com.wythe.mall.adapter.AreaListAdapter;
import com.wythe.mall.base.BaseActivity;
import com.wythe.mall.intf.OnRequestDataListener;
import com.wythe.mall.model.SizeBean;
import com.wythe.mall.utils.ApiOkHttp;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class AreaActivity extends BaseActivity {

    private AppCompatTextView areaTextView;
    private AppCompatTextView cityTextView;
    private AppCompatTextView provinceTextView;
    private RecyclerView areaRecyclerView;
    private RecyclerView cityRecyclerView;
    private RecyclerView provinceRecyclerView;

    private long exitTimeLong;
    private String areaNameString;
    private String cityNameString;
    private String provinceNameString;

    private AreaListAdapter areaAdapter;
    private ArrayList<String> areaArrayList;
    private AreaListAdapter cityAdapter;
    private ArrayList<String> cityArrayList;
    private AreaListAdapter provinceAdapter;
    private ArrayList<String> provinceArrayList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_area);
        if (provinceArrayList == null) {
            provinceArrayList = new ArrayList<>();
        }
        provinceArrayList.clear();
        if (cityArrayList == null) {
            cityArrayList = new ArrayList<>();
        }
        cityArrayList.clear();
        if (areaArrayList == null) {
            areaArrayList = new ArrayList<>();
        }
        areaArrayList.clear();
        initView();
        initData();
        initEven();
    }

    public void initView() {
        findViewById(R.id.title_left_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ((TextView) findViewById(R.id.titleText)).setText("选择地区");
        areaTextView = (AppCompatTextView) findViewById(R.id.areaTextView);
        cityTextView = (AppCompatTextView) findViewById(R.id.cityTextView);
        provinceTextView = (AppCompatTextView) findViewById(R.id.provinceTextView);
        areaRecyclerView = (RecyclerView) findViewById(R.id.areaRecyclerView);
        cityRecyclerView = (RecyclerView) findViewById(R.id.cityRecyclerView);
        provinceRecyclerView = (RecyclerView) findViewById(R.id.provinceRecyclerView);

    }

    public void initData() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        provinceAdapter = new AreaListAdapter(provinceArrayList);
        provinceRecyclerView.setLayoutManager(linearLayoutManager);//设置布局管理器
        provinceRecyclerView.setLayoutManager(linearLayoutManager);//设置布局管理器
        provinceRecyclerView.setAdapter(provinceAdapter);

        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(getActivity());
        linearLayoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        cityAdapter = new AreaListAdapter(cityArrayList);
        cityRecyclerView.setLayoutManager(linearLayoutManager1);//设置布局管理器
        cityRecyclerView.setLayoutManager(linearLayoutManager1);//设置布局管理器
        cityRecyclerView.setAdapter(cityAdapter);

        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(getActivity());
        linearLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
        areaAdapter = new AreaListAdapter(areaArrayList);
        areaRecyclerView.setLayoutManager(linearLayoutManager2);//设置布局管理器
        areaRecyclerView.setLayoutManager(linearLayoutManager2);//设置布局管理器
        areaRecyclerView.setAdapter(areaAdapter);
        getProvince("中国");

    }

    public void initEven() {

        provinceAdapter.setOnItemClickListener(new AreaListAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position, String bean) {
                areaTextView.setText("地区");
                cityTextView.setText("城市");
                provinceTextView.setText(bean);
                provinceNameString = bean;
                areaTextView.setTextColor(AreaActivity.this.getResources().getColor(R.color.greyAdd));
                cityTextView.setTextColor(AreaActivity.this.getResources().getColor(R.color.greyAdd));
                provinceTextView.setTextColor(AreaActivity.this.getResources().getColor(R.color.primary));
                getCity(bean);
            }
        });

        cityAdapter.setOnItemClickListener(new AreaListAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position, String bean) {
                areaTextView.setText("地区");
                cityNameString = bean;
                cityTextView.setText(cityNameString);
                areaTextView.setTextColor(AreaActivity.this.getResources().getColor(R.color.greyAdd));
                cityTextView.setTextColor(AreaActivity.this.getResources().getColor(R.color.primary));
                getArea(bean);
            }
        });

        areaAdapter.setOnItemClickListener(new AreaListAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position, String bean) {
                areaNameString = bean;
                Intent intent = new Intent();
                intent.putExtra("area_name", areaNameString);
                intent.putExtra("city_name", cityNameString);
                intent.putExtra("province_name", provinceNameString);
                intent.putExtra("area_info", provinceNameString + " " + cityNameString + " " + areaNameString);
                AreaActivity.this.setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });
    }

    //自定义方法

    private void getArea(String key) {

        JSONObject param = new JSONObject();
        try {
            param.put("keywords", key);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ApiOkHttp.getProvinceList(this, param, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                try {
                    areaArrayList.clear();
                    JSONArray data1 = data.getJSONArray("data").getJSONObject(0).getJSONArray("districts");
                    for (int i = 0; i < data1.length(); i++) {
                        String name = data1.getJSONObject(i).getString("name");
                        areaArrayList.add(name);
                    }
                    areaAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });


    }

    private void getCity(String key) {

        JSONObject param = new JSONObject();
        try {
            param.put("keywords", key);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ApiOkHttp.getProvinceList(this, param, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                try {
                    cityArrayList.clear();
                    areaArrayList.clear();
                    JSONArray data1 = data.getJSONArray("data").getJSONObject(0).getJSONArray("districts");
                    for (int i = 0; i < data1.length(); i++) {
                        String name = data1.getJSONObject(i).getString("name");
                        cityArrayList.add(name);
                    }
                    cityAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });

    }

    private void getProvince(String key) {
        JSONObject param = new JSONObject();
        try {
            param.put("keywords", key);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ApiOkHttp.getProvinceList(this, param, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                try {
                    provinceArrayList.clear();
                    cityArrayList.clear();
                    areaArrayList.clear();
                    JSONArray data1 = data.getJSONArray("data").getJSONObject(0).getJSONArray("districts");
                    for (int i = 0; i < data1.length(); i++) {
                        String name = data1.getJSONObject(i).getString("name");
                        provinceArrayList.add(name);
                    }
                    provinceAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });
    }

}
